import random
from flask import Flask,Blueprint,request,make_response,Response
from model.model import *
import string,re,json,redis,time
import os
from tools.tools import my_router,item2dict
from datetime import datetime
import arrow
from config import DAYS

houtilal = Blueprint('houtilal',__name__)

# 增加房间
@houtilal.route('/add_room',methods=['post'])
def add_room():
    room = request.json
    db.session.add(Room(**room))
    db.session.commit()
    return my_router(200)

# 获取当前空闲的房间
@houtilal.route('/get_room/',methods=['get'])
def get_room():
    room = Room.query.filter(Room.status == '空闲').all()
    room_list = []
    for i in room:
        room_list.append(item2dict(i))
    return my_router(200,room_list)

#修改房屋信息
@houtilal.route('/update_room/<int:r_id>',methods=['put'])
def put_room(r_id):
    room = request.json
    Room.query.filter_by(id=r_id).update(room)
    db.session.commit()
    return my_router(200)

# 删除房屋信息
@houtilal.route('/delete_room/<int:r_id>',methods=['delete'])
def delete_room(r_id):
    Room.query.filter_by(id=r_id).delete()
    db.session.commit()
    return my_router(200)

# 通过订单查询房间所属
@houtilal.route('/get_room/<int:o_id>',methods=['get'])
def get_room_by_order_id(o_id):
    room = Room.query.filter_by(order_id=o_id).all()
    room_list = []
    for i in room:
        room_list.append(item2dict(i))
    return my_router(200,room_list)

# 添加订单
@houtilal.route('/add_order',methods=['post'])
def add_order():
    order = request.json
    db.session.add(Order(**order))
    db.session.commit()
    return my_router(200)

# 查询订单
@houtilal.route('/get_all_order',methods=['get'])
def get_all_order():
    order = Order.query.filter().all()
    order_list = []
    for i in order:
        order_list.append(item2dict(i))
    return my_router(200,order_list)

# 通过人来获取
@houtilal.route('/update_order/<int:o_id>',methods=['update'])
def get_order_by_user_id(u_id):
    order = Order.query.filter_by(id=u_id).all()
    order_list = []
    for i in order:
        order_list.append(item2dict(i))
    return my_router(200,order_list)

# 直接更改指定的订单
@houtilal.route('/update_order/<int:o_id>',methods=['update'])
def update_order(o_id):
    order = request.json
    Order.query.filter_by(id=o_id).update(order)
    db.session.commit()
    return my_router(200)

# 删除订单
@houtilal.route('/delete_order/<int:o_id>',methods=['delete'])
def delete_order(o_id):
    Order.query.filter_by(id=o_id).delete()
    db.session.commit()
    return my_router(200)

# 获取所有的订单并且排序
@houtilal.route('/house/')
def get_order():
    # 按时间倒序显示订单信息
    order = Order.query.all()
    order_list = item2dict(order)
    r = sorted(order_list,key=lambda x:x.get('start_time'))
    return my_router(200,r)

# 获取单个房子的所有订单并判断时间
@houtilal.route('/house22/<int:h_id>',methods=['get'])
def get_one_house_order(h_id):
    house = Order.query.filter_by(house_id=h_id).all()
    time_1 = datetime.now().timestamp()
    list1 = []
    for i in house:
        if i.end_time > int(time_1):
            list1.append(item2dict(i))
    # 获取当前房子的订单
    # 今天以后
    return my_router(200,list1)

# 更新房子订单的状态
@houtilal.route('11/<int:o_id>',methods=['PUT'])
def put_house_order(o_id):
    # 拿到房东操作的一个字段
    stauts = request.json
    try:
        Order.query.filter_by(id=o_id).update(stauts)
    except Exception as e:
        print(e)
    db.session.commit()
    # 修改订单状态
    return my_router(200)

# 房间入住率
@houtilal.route('/stacisics/occupency')
def occupency():
    year_now = arrow.now().year
    start_t = arrow.get(f'{year_now}-01-01 00:00:00','YYYY-MM-DD HH:mm:ss')
    endin_t = arrow.get(f'{year_now}-12-31 24:00:00','YYYY-MM-DD HH:mm:ss')
    bettwon = endin_t - start_t
    total = Room.query.count() * bettwon.days
    orders = Order.query.filter(Order.start_time < endin_t.int_timestamp,Order.end_time > start_t.int_timestamp).all()
    days = 0
    for order in orders:
        days += (order.end_time - order.start_time) // DAYS
    return '%.3f'%(days * 100 / total) + '%'

# 月租金额的统计
@houtilal.route('/all_price',methods=['get'])
def get_room_price():
    year_now = arrow.now().year
    start_t = arrow.get(f'{year_now}-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss')
    print(start_t)
    endin_t = arrow.get(f'{year_now}-01-30 24:00:00', 'YYYY-MM-DD HH:mm:ss')
    orders = Order.query.filter(Order.start_time < endin_t.int_timestamp,Order.end_time > start_t.int_timestamp).all()
    money = 0
    for i in orders:
        money += (i.end_time - i.start_time) + i.price
    return my_router(200,f'酒店一月的金额是{money}￥')

# 增加广告
@houtilal.route('/add_advert',methods=['post'])
def add_advert():
    data = request.json
    if not data:
        return my_router(409)
    db.session.add(Advert(**data))
    db.session.commit()
    return my_router(200)

# 查看广告
@houtilal.route('/get_advert')
def get_advert():
    ar = Advert.query.all()
    return my_router(200,item2dict(ar))

# 更改广告
@houtilal.route('/update_advert/<int:a_id>',methods=['put'])
def update_advert(a_id):
    av = request.json
    Advert.query.filter_by(id=a_id).update(av)
    db.session.commit()
    return my_router(200)

# 删除广告
@houtilal.route('/delete_advert/<int:a_id>',methods=['delete'])
def delete_advert(a_id):
    Advert.query.filter_by(id=a_id).delete()
    db.session.commit()
    return my_router(200)

# 增加设施
@houtilal.route('/add_facility',methods=['post'])
def add_facility():
    fac = request.json
    db.session.add(Facility(**fac))
    db.session.commit()
    return my_router(200)

# 获取设施
@houtilal.route('/get_facility',methods=['get'])
def get_facility():
    fac = Facility.query.all()
    return my_router(200,item2dict(fac))

# 修改设施
@houtilal.route('/update_facility/<int:f_id>',methods=['put'])
def update_facility(f_id):
    fac = request.json
    Facility.query.filter_by(id=f_id).update(fac)
    db.session.commit()
    return my_router(200)

# 删除设施
@houtilal.route('/delete_facility/<int:f_id>',methods=['delete'])
def delete_facility(f_id):
    Facility.query.filter_by(id=f_id).delete()
    db.session.commit()
    return my_router(200)
